package com.shengwei.truck.driver.view.ui.mine.msgCenter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.lifecycle.Observer;

import com.shengwei.truck.driver.R;
import com.shengwei.truck.driver.bean.Result;
import com.shengwei.truck.driver.databinding.MsgCenterActivityBinding;
import com.shengwei.truck.driver.view.adapter.mine.MsgFragmentPagerAdapter;
import com.shengwei.truck.driver.view.ui.BaseActivity;
import com.shengwei.truck.driver.vm.MsgListViewModel;

/**
 * 消息中心页面，包含2个TAB标签，一个是系统消息，一个是转派与共享消息
 */
public class MsgCenterActivity extends BaseActivity<MsgListViewModel, MsgCenterActivityBinding> implements View.OnClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化控件
        initView();
        initLiveDataObersve();
    }
    @Override
    public int getContentViewId() {
        return R.layout.msg_center_activity;
    }

    /**
     * 初始化控件
     */
    public void initView() {
        //toolbar最右边的button，清除未读
        Button btClearAllUnread = this.findViewById(R.id.btRight);
        btClearAllUnread.setVisibility(View.VISIBLE);
        btClearAllUnread.setBackground(null);
        btClearAllUnread.setText(R.string.clear_unread);
        btClearAllUnread.setOnClickListener(this);
        //tablayout+viewpager+fragment实现可滑动的任务列表TAB页
        MsgFragmentPagerAdapter msgFragmentPagerAdapter = new MsgFragmentPagerAdapter(this,getSupportFragmentManager());
        mDataBinding.viewPager.setAdapter(msgFragmentPagerAdapter);
        mDataBinding.tablayout.setupWithViewPager(mDataBinding.viewPager);
        mDataBinding.viewPager.setCurrentItem(0);
    }
    /**
     * 创建liveData观察者，用于监听数据变化，从而更新UI
     */
    private void initLiveDataObersve() {
        mViewModel.getHasReadAllLiveData().observe(this, new Observer<Result<String>>() {
            @Override
            public void onChanged(Result<String>searchUserDataResult) {
                searchUserDataResult.handler(new OnCallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        Toast.makeText(MsgCenterActivity.this,R.string.clear_all_ok,Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btRight) {
            //清除所有 未读
            mViewModel.postHasRead("0",1);
        }
    }
}
